
from loguru import logger
from socketd import SocketD
from socketd.broker.BrokerListener import BrokerListener
from socketd.transport.core.Message import Message
from socketd.transport.core.Session import Session
from socketd.transport.server.Server import Server
from socketd.transport.server.ServerConfig import ServerConfig
from websockets import WebSocketServer



class BrokerListenerEx(BrokerListener):

    async def on_open(self, session: Session):
        logger.info("Broker open")
        return await super().on_open(session)

    def on_close(self, session: Session):
        super().on_close(session)

    async def on_message(self, requester: Session, message: Message):
        return await super().on_message(requester, message)

    def on_error(self, session: Session, error):
        super().on_error(session, error)

async def start():
    server: Server = SocketD.create_server(ServerConfig("ws").port(9999))
    server_session: WebSocketServer = await server.listen(BrokerListenerEx()).start()
    return server_session